package com.py.credit.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.py.credit.dao.GatewayUserDao;
import com.py.credit.entity.GatewayUserEntity;
import com.py.credit.service.GatewayUserService;
import com.py.credit.utils.RestTemplateUtils;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.UrlHelper;


/**
 * 用户管理Service实现
 *
 * @author Leegern
 * @date 2018年4月25日
 */
@Service
public class GatewayUserServiceImpl implements GatewayUserService {

	@Autowired
	private GatewayUserDao gatewayUserDao;
	@Autowired
    private UrlHelper urlHelper;
	@Override
	public GatewayUserEntity getUserByTwoPrincipal(Map<String, Object> params) {
		return gatewayUserDao.selectUserByTwoPrincipal(params);
	}

	@Override
	public void addUserInfo(GatewayUserEntity entity) {
		gatewayUserDao.addUserInfo(entity);
	}

	@Override
	public void fileUpload(MultipartFile file, String pathFile, String prefix)
			throws IllegalStateException, IOException {
		File uploadFile = null;

		File dirPath = new File(pathFile);
		uploadFile = new File(pathFile + File.separator + prefix + file.getOriginalFilename());

		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}

		try {
			file.transferTo(uploadFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<LinkedHashMap> getXypjNotice(long userId) {
		return gatewayUserDao.getXypjNotice(userId);
	}

	@Override
	public void modifyUserInfo(Map<String, Object> params) {
		gatewayUserDao.modifyUserInfo(params);
	}

	@Override
	public void setDefalutIcon(String destPath, String iconName) throws IOException {
		File dirPath = new File(destPath);

		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}

		// 打开输入流
		FileInputStream fis = new FileInputStream(
				GatewayUserServiceImpl.class.getClassLoader().getResource("images/" + iconName).getPath());
		// 打开输出流
		FileOutputStream fos = new FileOutputStream(destPath + File.separator + "icon.png");

		// 读取和写入信息
		int len = 0;
		// 创建一个字节数组，当做缓冲区
		byte[] b = new byte[1024];
		while ((len = fis.read(b)) != -1) {
			fos.write(b, 0, len);
		}

		// 关闭流 先开后关 后开先关
		fos.close(); // 后开先关
		fis.close(); // 先开后关
	}

	@Override
	public List<LinkedHashMap> getZjPhotoListForPage(long userId, Page page) {
		return gatewayUserDao.getZjPhotoListForPage(userId, page);
	}

	@Override
	public LinkedHashMap getZjPhotoInfo(long id) {
		return gatewayUserDao.getZjPhotoInfo(id);
	}

	@Override
	public void addZjPhotoInfo(String zjName, String zjdw, String type, String filepath, String startTime,
			String endTime, long userId, String typeValue, String status) {
		gatewayUserDao.addZjPhotoInfo(zjName, zjdw, type, filepath, startTime, endTime, userId, typeValue, status);
	}

	@Override
	public void modifyZjPhotoInfo(String zjName, String zjdw, String type, String filepath, String startTime,
			String endTime, long id, String typeValue) {
		gatewayUserDao.modifyZjPhotoInfo(zjName, zjdw, type, filepath, startTime, endTime, id, typeValue);
	}

	@Override
	public void delZjPhotoInfo(long id) {
		gatewayUserDao.delZjPhotoInfo(id);
	}

	@Override
	public int selectUserIsSensitiveAccount(String username) {
		return gatewayUserDao.selectUserIsSensitiveAccount(username);
	}

	@Override
	public BigInteger getUserInfo(String bm) {

		return gatewayUserDao.getUserInfo(bm);
	}

	@Override
	public GatewayUserEntity getUserInfoById(String id) {
		return gatewayUserDao.getUserInfoById(id);
	}

	@Override
	public void repeatModifyUserInfo(GatewayUserEntity entity) {
		gatewayUserDao.repeatModifyUserInfo(entity);

	}

	@Override
	public List<LinkedHashMap> selectApplicatonReportListForPage(String sqr, String reportType, String reportStatus,
			String region, String industry, String startTime, String endTime, Page page) {
		return gatewayUserDao.selectApplicatonReportListForPage(sqr, reportType, reportStatus, region, industry,
				startTime, endTime, page);

	}

	@Override
	public Map<String, Object> checkFromplatform(String tablename, GatewayUserEntity entity,String type) {
		String url =  urlHelper.getPlatformServerUrl() +"/creditViewController/checkfromPlatform.json";
		MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 params.add("ztmc", entity.getZtmc());
		 params.add("bm", entity.getBm());
		 params.add("fddbr", entity.getFddbr());
		 params.add("fddbrzjhm", entity.getFddbrzjhm());
		 params.add("tablename", tablename);
		 params.add("type", type);
		  //获取数据
        ResponseEntity<String> data = RestTemplateUtils.sendPost(url, params);
        Map<String, Object> objectMap = JSONObject.parseObject(data.getBody());   
       
	            return objectMap;
	      
	}
}